/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * V1ModPersoAcept.java
 *
 * Created on 14-may-2012, 12:44:04
 */
package practtica4;

import BBDD.GestorBBDD;
import DTO.Alias;
import DTO.Cargo;
import DTO.Personaje;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario_local
 */
public class V1ModPersoAcept extends javax.swing.JFrame {
    GestorBBDD gestor=new GestorBBDD();
    private Personaje personaje;
    /** Creates new form V1ModPersoAcept */
    public V1ModPersoAcept() {
        initComponents();
    }

    V1ModPersoAcept(Personaje per) {
        this.personaje=per;
        initComponents();
        cargarEtiquetas();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonBio = new javax.swing.JTextField();
        botonId = new javax.swing.JTextField();
        botonEliminar = new javax.swing.JButton();
        botonNombre = new javax.swing.JTextField();
        botonLugMuerte = new javax.swing.JTextField();
        botonFechaNac = new javax.swing.JTextField();
        botonFechaMuerte = new javax.swing.JTextField();
        labelLugNac = new javax.swing.JLabel();
        labelFechaMuerte = new javax.swing.JLabel();
        botonApellido = new javax.swing.JTextField();
        labelLugMuerte = new javax.swing.JLabel();
        botonAceptar = new javax.swing.JButton();
        labelApellido = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        labelFechaNac = new javax.swing.JLabel();
        labelId = new javax.swing.JLabel();
        botonLugNac = new javax.swing.JTextField();
        labelBio = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        botoncargo = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        botonalias = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        botonEliminar.setText("Eliminar");
        botonEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonEliminarMouseClicked(evt);
            }
        });

        botonFechaNac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonFechaNacActionPerformed(evt);
            }
        });

        labelLugNac.setText("Lugar nacimiento");

        labelFechaMuerte.setText("Fecha muerte");

        labelLugMuerte.setText("Lugar muerte");

        botonAceptar.setText("Aceptar");
        botonAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botonAceptarMouseClicked(evt);
            }
        });

        labelApellido.setText("apellido");

        labelNombre.setText("nombre");

        labelFechaNac.setText("Fecha nacimiento");

        labelId.setText("id");

        labelBio.setText("Biografía");

        jLabel1.setText("Alias");

        jLabel2.setText("Cargos-Fecha inicio-Fecha fin");

        botoncargo.setColumns(20);
        botoncargo.setRows(5);
        jScrollPane1.setViewportView(botoncargo);

        botonalias.setColumns(20);
        botonalias.setRows(5);
        jScrollPane2.setViewportView(botonalias);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAceptar)
                        .addGap(0, 284, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(162, 162, 162)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botonFechaMuerte, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(botonNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                        .addGap(65, 65, 65))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(botonBio)
                                                .addComponent(botonFechaNac)
                                                .addComponent(botonApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(botonLugMuerte, javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(botonLugNac, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                                            .addComponent(botonId, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 105, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelFechaNac)
                                    .addComponent(labelBio)
                                    .addComponent(labelFechaMuerte)
                                    .addComponent(labelNombre)
                                    .addComponent(labelApellido)
                                    .addComponent(labelId)
                                    .addComponent(labelLugMuerte)
                                    .addComponent(labelLugNac)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(44, 44, 44)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 22, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelId)
                    .addComponent(botonId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(botonNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelApellido))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelBio)
                    .addComponent(botonBio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFechaNac)
                    .addComponent(botonFechaNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFechaMuerte)
                    .addComponent(botonFechaMuerte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLugNac)
                    .addComponent(botonLugNac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLugMuerte)
                    .addComponent(botonLugMuerte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonAceptar)
                            .addComponent(botonEliminar)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 108, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonAceptarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonAceptarMouseClicked
        // TODO add your handling code here:
        
       // personaje.setId(Integer.parseInt(this.botonId.getText()));-->No permito cambiar el id
        personaje.setNombre(this.botonNombre.getText());
        personaje.setApellidos(this.botonApellido.getText());
        personaje.setBiografia(this.botonBio.getText());
       
        String fechanac =  this.botonFechaNac.getText(); 
        String fechamuerte =  this.botonFechaMuerte.getText(); 
//        java.text.SimpleDateFormat formato = new java.text.SimpleDateFormat("yyyy/MM/dd"); 
//        java.util.Date fechaDateN = null;
//        java.util.Date fechaDateM = null; 
//        try {
//            fechaDateN = formato.parse(fechanac);
//            fechaDateM = formato.parse(fechamuerte);
//        } catch (ParseException ex) {
//            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
//        }
        personaje.setFechaNac(fechanac);
        personaje.setFechafMuerte(fechamuerte);
        personaje.setLugarNac(this.botonLugNac.getText());
        personaje.setLugarMuerte(this.botonLugMuerte.getText());
        
        String alias= this.botonalias.getText();
        ArrayList<Alias> al=personaje.getAlias();
        int fin=0;
        int ini=0;
        int i=0;
        while(fin<alias.length()){
            ini=fin+1;
            fin= alias.indexOf('\n', ini);
            al.get(i).setNombre(alias.substring(ini, ini));
            i++;
        }
        
        String cargos= this.botonalias.getText();
        ArrayList<Cargo> car=personaje.getCargos();
        fin=0;
        ini=0;
        i=0;String aux;int a;int b;
        while(fin<cargos.length()){
            ini=fin+1;
            fin= cargos.indexOf('\n', ini);
            aux=cargos.substring(ini, ini);
            a=aux.indexOf(',',0);
            b=aux.indexOf(',',a);
            String fi = aux.substring(a, b);
            String ff =  aux.substring(b, fin); 
//            formato = new java.text.SimpleDateFormat("yyyy/MM/dd"); 
//            fechaDateN = null;
//            fechaDateM = null; 
//            try {
//                fechaDateN = formato.parse(fi);
//                fechaDateM = formato.parse(ff);
//            } catch (ParseException ex) {
//                Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
//            }
            car.get(i).setNombre(aux.substring(0,a));
            //No se modifica la descripcion
            car.get(i).setFI(fi);
            car.get(i).setFF(ff);
            
            i++;
        }
       
        personaje.setCargos(car);
        personaje.setAlias(al);
        try {
            gestor.guardarPersonaje(personaje);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
         v1.setVisible(true);
            this.dispose();
        
    }//GEN-LAST:event_botonAceptarMouseClicked

    private void botonEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonEliminarMouseClicked
        try {
            gestor.eliminarPersonaje(personaje);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V1ModPersoAcept.class.getName()).log(Level.SEVERE, null, ex);
        }
        V1 v1=new V1();
         v1.setVisible(true);
            this.dispose();
    }//GEN-LAST:event_botonEliminarMouseClicked

private void botonFechaNacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonFechaNacActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonFechaNacActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JTextField botonApellido;
    private javax.swing.JTextField botonBio;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JTextField botonFechaMuerte;
    private javax.swing.JTextField botonFechaNac;
    private javax.swing.JTextField botonId;
    private javax.swing.JTextField botonLugMuerte;
    private javax.swing.JTextField botonLugNac;
    private javax.swing.JTextField botonNombre;
    private javax.swing.JTextArea botonalias;
    private javax.swing.JTextArea botoncargo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelApellido;
    private javax.swing.JLabel labelBio;
    private javax.swing.JLabel labelFechaMuerte;
    private javax.swing.JLabel labelFechaNac;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelLugMuerte;
    private javax.swing.JLabel labelLugNac;
    private javax.swing.JLabel labelNombre;
    // End of variables declaration//GEN-END:variables

    private void cargarEtiquetas() {
        int id= personaje.getId();
        String id2=id+"";
        this.botonId.setText(id2);
        this.botonNombre.setText(personaje.getNombre());
        this.botonApellido.setText(personaje.getApellidos());
        this.botonBio.setText(personaje.getBiografia());
        String fn=personaje.getFechaNac().toString();
        String fm=personaje.getFechafMuerte().toString();
        this.botonFechaNac.setText(fn);
        this.botonFechaMuerte.setText(fm);
        this.botonLugNac.setText(personaje.getLugarNac());
        this.botonLugMuerte.setText(personaje.getLugarMuerte());
        int i=0;
        String cargos="";
        while(i<personaje.getCargos().size()){
            cargos+= personaje.getCargos().get(i).getNombre()+ ","+personaje.getCargos().get(i).getFI()+","+personaje.getCargos().get(i).getFF()+'\n';
            i++;
        }
        this.botoncargo.setText(cargos);
        String alias="";
        while(i<personaje.getAlias().size()){
            alias+= personaje.getAlias().get(i).getNombre()+'\n';
            i++;
        }
        this.botonalias.setText(alias);
        
        
        
    }
}
